<?php
set_time_limit(6000);
//连接MYSQL
$dsn = 'mysql:host=192.168.26.40;dbname=datacenter';
$user = 'datacenter';
$password = 'dddddd';
try {
    $pdo = new PDO($dsn, $user, $password);
} catch (PDOException $e) {
    echo $e->getMessage();
    exit;
}
$mysql = mysql_connect('192.168.26.40', 'datacenter', 'dddddd');
mysql_select_db('datacenter');

//连接mongodb
$mongoDB = new Mongo('mongodb://dbadmin:namFTo7q2m@www.swtor4credits.com');
$usfcenter = $mongoDB->USFCenter;

//游戏
/*
$games = $mongoDB->USFCenter->games->find();
foreach ($games as $game){
    //保存游戏
    $query = $pdo->prepare("INSERT INTO product_category (parentid, price, title) VALUES (0, 0, :title)");
    $query->execute(array(':title' => $game['game'].'-'.$game['game_area']));
    $game_id = $pdo->lastInsertId();
    //保存服务区
    foreach($game['game_server'] as $v){
        $query = $pdo->prepare("INSERT INTO product_category (parentid, price, title) VALUES (:parentid, :price, :title)");
        echo $query->execute(array(
            ':parentid' => $game_id,
            ':price' => (float)$v['gold_price'],
            ':title' => $v['name'],
        ));
    }
    
}
 * 
 */

//分站
/*
$sites = $usfcenter->sites->find();
foreach ($sites as $v){
    echo $v['site_domin'];
    $insert = $pdo->prepare("INSERT INTO web_site (site_key, domain, server_name, order_prefix, remit) VALUES (:siteKey, :domain, '', :order_prefix, '')");
    $insert->execute(array(
        ':siteKey' => $v['site_key'],
        ':domain' => $v['site_domin'],
        ':order_prefix' => $v['site_key'],
        ));
    echo $pdo->lastInsertId();
    $insert->closeCursor();
    echo "\n";
}
 * 
 */

//金币
/*
 $golds = $usfcenter->products->find(array('type' => 'gd'));
var_dump($golds->count());
exit;
foreach($golds as $v){
    //print_r($v);
    $siteQuery = mysql_query("SELECT * FROM web_site WHERE site_key='$v[WEBSITE]'");
    $site = mysql_fetch_array($siteQuery);
    
    $game = $usfcenter->games->findOne(array('id' => (int)$v['game']));
    //print_r($game);
    $query = mysql_query("SELECT * FROM product_category WHERE title='$game[game]' AND parentid=0");
    $newGame = mysql_fetch_array($query);
    $query = mysql_query("SELECT * FROM product_category WHERE title='$game[game_area]' AND parentid=$newGame[id]");
    $subGame = mysql_fetch_array($query);
    //print_r($subGame);
    //exit;
    $sql = "INSERT INTO product_gold (site_key, gold_game, gold_name, gold_num, gold_unit, gold_discount) VALUES ('$site[domain]', $subGame[id], '$v[NAME]', $v[NUM], '$v[UNIT]', $v[DISCOUNT])";
    echo mysql_query($sql);
    echo "\n";
    //exit;
}
 * 
 */

//等级代练
/*
$pls = $usfcenter->products->find(array('type' => 'pl'));
foreach ($pls as $v){
    //print_r($v);
    $game = $usfcenter->games->findOne(array('id' => (int)$v['game']));
    //print_r($game);
    $query = mysql_query("SELECT * FROM product_category WHERE title='$game[game]' AND parentid=0");
    $newGame = mysql_fetch_array($query);
    $query = mysql_query("SELECT * FROM product_category WHERE title='$game[game_area]' AND parentid=$newGame[id]");
    $subGame = mysql_fetch_array($query);
    //print_r($subGame);
    echo mysql_query("INSERT INTO product_powerleveling (pl_game, pl_level, pl_price, pl_time) VALUES ($subGame[id], $v[LEVEL], $v[PRICE], $v[NEED_TIME])");
    //echo "\n";
    //exit;
}
 * 
 */

//技能
/*
$skills = $usfcenter->products->group(array('SKILL' => 1), array("count" => 0), "function (obj, prev) { prev.count++; }", array('type' => 'sk'));
foreach($skills as $v)
{
    foreach($v as $sb){
        echo mysql_query("INSERT INTO product_skill (game_id, skill_name, order_sort) VALUES (67, '$sb[SKILL]', 0)");
    }
}
 * 
 */
//技能等级
$skillLevels = $usfcenter->products->find(array('type' => 'sk'));
foreach ($skillLevels as $v)
{
    
    $skill = $v['SKILL'];
    $query = mysql_query("SELECT * FROM product_skill WHERE skill_name='$skill'");
    $newSkill = mysql_fetch_array($query);
    $skill_id = $newSkill['id'];
    echo mysql_query("INSERT INTO product_skill_level (skill_id, skill_level, skill_time, skill_price, skill_gold) VALUES ($skill_id, $v[LEVEL], $v[NEED_TIME], $v[PRICE], $v[NEED_GOLD])");
}